package com.atguigu.gmall.gateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        //创建对象CorsConfiguration
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //设置跨域相关参数
        //1.设置请求方法：GET,POST,PUT,*
        corsConfiguration.addAllowedMethod("*");
        //2.设置请求头
        corsConfiguration.addAllowedHeader("*");
        //3.设置跨域:通常不给*
        corsConfiguration.addAllowedOrigin("*");
        //4.可以携带cookies
        corsConfiguration.setAllowCredentials(true);


        //返回当前对象urlBasedCorsConfigurationSource
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //配置哪些需要过滤的路径
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }

}
